package cn.kgc.crowdfundingadmin03.controller;

import cn.kgc.crowdfundingadmin03.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin03.common.ResultEntity;
import cn.kgc.crowdfundingadmin03.pojo.Cert;
import cn.kgc.crowdfundingadmin03.service.CertService;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-28 14:27
 */
@RestController
public class CertController {

    @Resource
    CertService certService;

    @RequestMapping("/cert/search/by/keyword")
    public ResultEntity<PageInfo<Cert>> search(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5", required = false) Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "", required = false) String keyword,
            Model model
    ) {
        PageInfo<Cert> pageInfo = certService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        //2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }

    //根据id查询 删除ajax前置
    @RequestMapping("/cert/get/list/by/id/list")
    public ResultEntity<List<Cert>> getCertListByIdList(@RequestBody List<Integer> certIdList) {
        List<Cert> certListByIdList = certService.getCertListByIdList(certIdList);
        return ResultEntity.successWithoutData(certListByIdList);
    }

    //删除
    @RequestMapping("/cert/batch/remove")
    public ResultEntity<String> getRoleListByIdList(@RequestBody List<Integer> certIdList) {
        certService.batchRemove(certIdList);
        return ResultEntity.successWithoutData();
    }

    //增加  ajax 传值增加
    @RequestMapping("/cert/save/role")
    public ResultEntity<String> saveRole(@RequestParam("roleName") String roleName) {
        certService.saveCert(roleName);
        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/cert/update/role")
    public ResultEntity<String> updateRole(Cert cert) {
        certService.updateCert(cert);
        return ResultEntity.successWithoutData();
    }
}
